<?php

namespace App\Http\Controllers\Wechat;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Route;
use App\Http\Requests\Wechat\MsgRuleRequest;
use App\Repositories\BLL\MsgRuleBLL;
use App\Models\Wechat\MsgRule;

class MsgRuleController extends Controller
{
    private $msgRuleBLL;
    private $currentRouteName;

    public function __construct(MsgRuleBLL $msgRule)
    {
        $this->currentRouteName = Route::currentRouteName();
        $this->msgRuleBLL = $msgRule;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $addButton = sp_get_AddActionButton($this->currentRouteName);
        return view(
            'wechat.msgrule.index',
            ["addButton"=>$addButton]
        );

    }

    public function ajaxIndex()
    {
        $result = $this->msgRuleBLL->ajaxIndex($this->currentRouteName);
        return response()->json($result);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $msgRule = new MsgRule();
        return view('wechat.msgrule.create',
                    ['msgrule'=>$msgRule]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(MsgRuleRequest $request)
    {
        $result = $this->msgRuleBLL->createEntity($request->all());
        return redirect(route('msgrule.index'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $msgRule = $this->msgRuleBLL->editView($id);
        return view(
            'wechat.msgrule.show',
            ["msgrule"=>$msgRule]
        );
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $msgRule = $this->msgRuleBLL->editView($id);
        return view(
            'wechat.msgrule.edit',
            ["msgrule"=>$msgRule]
        );
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $result = $this->msgRuleBLL->updateEntity($request->all(),$id);
		if($result){
			flash("更新成功");
		}else{
			flash("更新失败","error");
		}
        return redirect(route('msgrule.index'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->msgRuleBLL->destroyEntity($id);
        return redirect(route('msgrule.index'));
    }
}
